package com.oa.service.impl;



import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.oa.base.BaseDaoImpl;
import com.oa.dao.TopicDao;
import com.oa.entity.Forum;
import com.oa.entity.Topic;
import com.oa.service.TopicService;

@Service("topicService")
@Transactional
public class TopicServiceImpl extends BaseDaoImpl<Topic> implements TopicService {

	//通过注解把TopicDao注入进来
	@Resource(name="topicDao")
	private TopicDao topicDao;
	
	@SuppressWarnings("static-access")
	public boolean addTopic(Topic topic) {
		topic.setPostingTime(new Date());
		topic.setTopicType(topic.TYPE_NORMAL);
		topic.setReplyCount(0);
		topic.setLastReply(null);
		topic.setLastUpdateTime(topic.getPostingTime());
		topicDao.saveEntity(topic);
		
		Forum forum = topic.getForum();
		forum.setArticleCount(forum.getArticleCount() + 1);
		forum.setTopicCount(forum.getTopicCount() + 1);
		forum.setLastTopic(topic);
		return topicDao.updatetopic(forum);
	}

	public boolean updateTopic(Topic topic) {
		return topicDao.updateEntity(topic);
	}

	public boolean deleteTopic(Topic topic) {
       Forum forum = topic.getForum();
		forum.setArticleCount(forum.getArticleCount()-topic.getReplyCount()- 1);
		forum.setTopicCount(forum.getTopicCount() - 1);
		forum.setLastTopic(null);
		topicDao.updatetopic(forum);
		 return  topicDao.deleteEntity(topic);
	}

	public List<Topic> selectAllTopic(String forumId) {
		List<Topic> pagerTopic=topicDao.queryAllTopicById(forumId);
		return pagerTopic;
	}

	public Topic selectTopic(String topicId) {
		Topic topic = topicDao.loadEntity(topicId);
		return topic;
	}

	public Topic selectTopicName(String postingTitle) {
		Topic topic = topicDao.queryTopicByName(postingTitle);
		return topic;
	}

	public Topic selectLastTopic(String forumId) {
		Topic topic = topicDao.queryLastTopic(forumId);
		return topic;
	}

	public TopicDao getTopicDao() {
		return topicDao;
	}

	public void setTopicDao(TopicDao topicDao) {
		this.topicDao = topicDao;
	}
    
}
